<template>
  <div class="view-fill">
    <div class="app-container-flex" v-if="!modifyInfo.visible">
      <NewFullScreen
        v-model="showDialog"
        :title="title"
        :btnList="fullInfo.btnList"
        :needOperateAffair="true"
        :operateAffairData="affairTableInfo"
        @returnBack="$emit('returnBack')"
      >
        <template v-slot:full-button>
          <el-button
            v-if="
              $permission('pendCheckProduct:update') &&
              infoList.data.checkStatus === 1
            "
            :plain="true"
            @click="update"
            >修改</el-button
          >
          <el-button
            v-if="
              $permission('pendCheckProduct:confirm') &&
              infoList.data.checkStatus === 1
            "
            :plain="true"
            @click="confirm"
            >确认</el-button
          >
          <el-button
            v-if="
              $permission('pendCheckProduct:refuse') &&
              infoList.data.checkStatus === 1
            "
            :plain="true"
            @click="refuse"
            >拒绝</el-button
          >
        </template>

        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="infoList" />
          </div>
        </div>
      </NewFullScreen>
    </div>
    <modify-pend-check-product-detail
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :id="modifyInfo.id"
      @returnBack="init"
    />
  </div>
</template>

<script setup name="pendCheckProductDetail">
import BasicInformation from '@/components/BasicInformation/index'
import { getProductCheck } from '@/api/core'
import PageTable from '@/components/PageTable/index'
import {
  productCheckConfirm,
  productCheckRefuse,
  getCoreProductRule,
} from '@/api/core'
import { orderOpAffairAll } from '@/api/affair'
import ModifyPendCheckProductDetail from './modify'

const { proxy } = getCurrentInstance()
const emit = defineEmits()

const props = defineProps(['visible', 'id'])

const title = ref('待审核产品详情')
const infoList = reactive({
  data: {
    isConsignment: 0,
    withOperateTable: 0,
  },
  fieldList: [
    { label: '证号', value: 'licenseNumber', type: 'text' },
    { label: '证照名称', value: 'licenseName', type: 'text' },
    { label: '英文名称', value: 'enName', type: 'text' },
    { label: '证照生效日期', value: 'licenseBeginDate', type: 'text' },
    { label: '证照失效日期', value: 'licenseEndDate', type: 'text' },
    { label: '包装规格', value: 'packedStandard', type: 'text' },
    { label: '通用名称', value: 'commonName', type: 'text' },
    { label: '编码', value: 'code', type: 'text' },
    { label: 'his编码', value: 'hisCode', type: 'text' },
    { label: '别名', value: 'alias', type: 'text' },
    { label: '助记符', value: 'mnemonic', type: 'text' },
    { label: '生产厂家', value: 'brandName', type: 'text' },
    { label: '产品类型', value: 'productTypeValue', type: 'text' },
    { label: '数量操作模式', value: 'qtyOperateModeValue', type: 'text' },
    {
      label: '单位转换系数',
      value: 'unitConvertCoefficient',
      type: 'text',
    },
    { label: '产品规格', value: 'standard', type: 'text' },
    {
      label: '近效期（天）',
      value: 'productNearlyEffective',
      type: 'text',
    },
    { label: '包装规格', value: 'packingSpecification', type: 'text' },
    { label: '计量单位', value: 'unitName', type: 'text' },
    { label: '计算单位', value: 'calcUnitName', type: 'text' },
    { label: '状态', value: 'statusValue', type: 'text' },
    { label: '财务科目', value: 'financialAccountValue', type: 'text' },
    { label: '条码类型', value: 'codeTypeValue', type: 'text' },
    { label: '原厂主码', value: 'mainCode', type: 'text' },
    {
      label: '需要批号效期',
      value: 'needBatchNumberValue',
      type: 'text',
    },
    { label: '手术跟台', value: 'withOperateTableValue' },
    { label: '以领定消', value: 'acquisitionReplaceConsumeValue' },
    { label: '防疫物资', value: 'epidemicPreventionValue' },
    { label: '临采产品', value: 'temporaryPurchValue' },
    { label: '是否议价', value: 'isBargainingValue' },
    { label: '是否挂网', value: 'isSunPurchaseValue' },
    { label: '挂网类型', value: 'networkTypeValue', type: 'text' },
    { label: '商品代码', value: 'serialNum', type: 'text' },
    { label: '是否收费', value: 'isChargeValue' },
    { label: '是否寄售', value: 'isConsignmentValue' },
    { label: '结算方式', value: 'settlementMethodValue' },
    { label: '限制价格', value: 'isLimitPriceValue' },
    { label: '采购限制价格', value: 'purchaseLimitPrice' },
    { label: '上架状态', value: 'putShelfStatusValue' },
    { label: '厂家理论测试次数', value: 'expectedUsageTimes' },
    { label: '发票类别', value: 'invoiceCategoryValue' },
    { label: '病案类别', value: 'caseCategoryValue' },
    { label: '质管类型', value: 'dosageFormValue', type: 'text' },
    {
      label: '库存类别',
      value: 'inventoryCategoriesValue',
      type: 'text',
    },
    { label: '产品分类', value: 'productCategoryValue', type: 'text' },
    { label: '国家医保编码', value: 'nationCode', type: 'text' },
    { label: '省医保编码', value: 'provinceCode', type: 'text' },
    {
      label: '国家医保名称',
      value: 'nationHealthInsurance',
      type: 'text',
    },
    { label: '原厂码规则', value: 'originTypeValue', type: 'text' },
    {
      label: '产品长度(cm)',
      value: 'productLength',
      type: 'text',
    },
    {
      label: '产品宽度(cm)',
      value: 'productWidth',
      type: 'text',
    },
    {
      label: '产品高度(cm)',
      value: 'productHeight',
      type: 'text',
    },
    { label: '创建人', value: 'createUserName' },
    { label: '创建时间', value: 'createTime' },
    { label: '最近更新人', value: 'updateUserName' },
    { label: '最近更新时间', value: 'updateTime' },
    { label: '备注', value: 'remark', type: 'popover' },
  ],
})
const affairTableInfo = reactive({
  api: orderOpAffairAll,
  query: {
    orderType: 81,
    orderId: props.id,
    sort: 'operate_time desc',
  },
})
const modifyInfo = reactive({
  visible: false,
  title: '修改产品信息',
  id: '',
})
const fullInfo = reactive({
  btnList: [],
})

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emit('update:visible', newValue) // 更新父组件visible
  },
})

watch(
  () => props.visible,
  (newVal) => {
    showDialog.value = newVal
  },
  {
    immediate: true,
  }
)

watch(
  () => props.id,
  () => {
    init()
  },
  {
    immediate: true,
    deep: true,
  }
)
watch(
  () => infoList.data.isConsignmentValue,
  (value) => {
    if (value == '是') {
      showFieldListItem(true, 'withOperateTableValue')
    } else {
      showFieldListItem(false, 'withOperateTableValue')
    }
  }
)

function showFieldListItem(show, nameValue) {
  let fieldList = infoList.fieldList
  fieldList.forEach((item) => {
    if (nameValue === item.value) {
      item.show = show
    }
  })
}
function update() {
  modifyInfo.visible = true
  modifyInfo.type = 'update'
  modifyInfo.id = props.id
  console.log(modifyInfo, 'modifyInfo')
}
//确认
function confirm() {
  const ids = [infoList.data.id]
  productCheckConfirm(ids).then((resp) => {
    if (resp && resp.success) {
      init()
    }
  })
}
//拒绝
function refuse() {
  const ids = [infoList.data.id]
  productCheckRefuse(ids).then((resp) => {
    if (resp && resp.success) {
      init()
    }
  })
}

function getGSRule(id) {
  getCoreProductRule(id).then((res) => {
    if (res && res.success && JSON.stringify(res.data) !== '{}') {
      infoList.data = {
        ...infoList.data,
        g1Type: res.data.type === 'GS1_OTHER' ? '自定义规则' : 'GS1规则',
      }
    }
  })
}
function init() {
  getProductCheck(props.id).then((res) => {
    if (res) {
      infoList.data = res.data
      getGSRule(res.data.id)
    }
  })
}
</script>

<style scoped></style>
